package work.timejay.user.controller.books;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import work.timejay.common.ApiPageEntity;
import work.timejay.common.ApiResult;
import work.timejay.user.dto.books.UserBookSaveDTO;
import work.timejay.user.dto.books.UserBooksPageDTO;
import work.timejay.user.entity.books.UserBooks;
import work.timejay.user.service.books.UserBooksService;
import work.timejay.user.vo.books.UserBooksPageVO;

/**
 * <p>
 * 用户书本表 前端控制器
 * </p>
 *
 * @author 王文杰
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/user-books")
public class UserBooksController {

    @Autowired
    private UserBooksService userBooksService;

    /**
     * 添加用户book
     */
    @PostMapping()
    public ApiResult<Void> saveBook(@RequestBody @Validated UserBookSaveDTO dto) {
        userBooksService.saveBook(dto);
        return ApiResult.success();
    }

    /**
     * 用户书本分页接口
     */
    @GetMapping(value = "/page")
    public ApiResult<ApiPageEntity<UserBooksPageVO>> userBooksPage(@Validated UserBooksPageDTO<UserBooks> dto) {
        ApiPageEntity<UserBooksPageVO> page = userBooksService.userBooksPage(dto);
        return ApiResult.success(page);
    }

    /**
     * 用户上传书籍
     */
    @PostMapping(value = "/upload")
    private ApiResult<Void> uploadBooks(@RequestPart(value = "file") MultipartFile file) {
        userBooksService.uploadBooks(file);
        return ApiResult.success();
    }
}

